package com.wanfu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.wanfu.entity.Article;
import com.wanfu.service.ArticleService;
import com.wanfu.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
public class ArticleController extends BaseController {

    @Resource
    ArticleService articleService;

    /**
     * 获取最新资讯
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresRoles("user")
    @RequestMapping("/article/listByTime")
    public Object listByTime(int pageNum, int pageSize){
        PageInfo<Article> page = articleService.listByTime(pageNum, pageSize);
        if(null != page.getList()) {
            return Result.ok(page);
        }
        return Result.error("获取数据失败，请稍后再试");
    }

    /**
     * 根据分类获取最新资讯
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @RequestMapping("/article/listByCategory")
    public Object listByCategory(int pageNum, int pageSize, int categoryId){
        PageInfo<Article> page = articleService.listByCategory(pageNum, pageSize, categoryId);
        if(null != page.getList()) {
            return Result.ok(page);
        }
        return Result.error("获取数据失败，请稍后再试");
    }

    /**
     * 添加资讯
     * @param article
     * @return
     */
    @RequestMapping("/article/add")
    public Object add(Article article){
        int row = articleService.add(article);
        if (row == 1) {
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }
    //修改
    @RequestMapping("/article/get")
    public Object get(int id){
        int row = articleService.select(id);
        if(row==1){
            return Result.ok("修改");
        }
        return Result.error("错误");

    }
    //更新
    @RequestMapping("/article/updata")
    public Object updata (Article article){
        int row=articleService.updata(article);
        if (row==1){
            return Result.ok("查询");
        }
       return Result.error("");
    }
    //删除
    @RequestMapping("/article/delete")
    public Object delete(int id){
        int row=articleService.selete(id);
        if(row==1){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

}
